﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		QueryRegistration.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 //   File:            QueryRegistration.cs
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using Dexter.Repository.NHibernate.Extensions;
using Dexter.Repository.NHibernate.Helpers;

namespace Dexter.Repository.NHibernate.Queries {
	internal static class QueryRegistration {
		public static void RegisterRepositoryQueries ( this INHibernateHelper nhelper ) {
			nhelper.AddCategoryRepositoryQueries ( );
			nhelper.AddBlogRollRepositoryQueries ( );
			nhelper.AddCommentRepositoryQueries ( );
			nhelper.AddPageRepositoryQueries ( );
			nhelper.AddReferrerRepositoryQueries ( );
			nhelper.AddTrackBackRepositoryQueries ( );
			nhelper.AddUserRepositoryQueries ( );
			nhelper.AddEmailRepositoryQueries ( );
			nhelper.AddPostRepositoryQueries ( );
			nhelper.AddTagRepositoryQueries ( );
			nhelper.AddRoleRepositoryQueries();
			nhelper.AddInboxRepositoryQueries ( );
			nhelper.AddLegacyurlRepositoryQueries();
			nhelper.AddPluginRepositoryQueries();
		}

		static void AddCategoryRepositoryQueries ( this INHibernateHelper configuration ) {
			configuration.Configuration.NamedQueries.RegisterQuery ( "select distinct c from Category c left join fetch c.Categories order by c.Name", KnownQueries.CategoryRepository.GetAllCategoriesWithChilds );
			configuration.Configuration.NamedQueries.RegisterQuery ( "select distinct c from Category c left join fetch c.Categories where c.Parent = null order by c.Name", KnownQueries.CategoryRepository.GetCategoriesStructureWithChilds );
			configuration.Configuration.NamedQueries.RegisterQuery ( "from Category c where c.IsDefaultCategory = true" , KnownQueries.CategoryRepository.GetDefaultCategory );
			configuration.Configuration.NamedQueries.RegisterQuery("from Category c left join fetch c.Categories where c.IsDefaultCategory = true", KnownQueries.CategoryRepository.GetDefaultCategoryWithChilds);
			configuration.Configuration.NamedQueries.RegisterQuery("from Category c left join fetch c.Categories where c.Id = :id", KnownQueries.CategoryRepository.GetCategoryByKeyWithChilds);
			configuration.Configuration.NamedQueries.RegisterQuery("from Category c left join fetch c.Categories where c.Name = :name", KnownQueries.CategoryRepository.GetCategoryByNameWithChilds);
			configuration.Configuration.NamedQueries.RegisterQuery("select distinct p from Post p join fetch p.Categories as cat where cat.Id = :catId order by p.PublishDate desc", KnownQueries.CategoryRepository.GetAllPostsIntoACategory);
			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(p) from Post p join p.Categories as cat where cat.Id = :catId", KnownQueries.CategoryRepository.GetPostCountIntoACategory );
		}

		static void AddBlogRollRepositoryQueries ( this INHibernateHelper configuration ) {
			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(Id) from BlogRoll" , KnownQueries.BlogRollRepository.GetBlogRollCount );
			configuration.Configuration.NamedQueries.RegisterQuery ( "from BlogRoll b order by b.Position" , KnownQueries.BlogRollRepository.GetBlogRollOrdedByPosition);
		}
		
		static void AddLegacyurlRepositoryQueries(this INHibernateHelper configuration)
		{
			configuration.Configuration.NamedQueries.RegisterQuery("select Count(Id) from LegacyUrl", KnownQueries.LegacyUrlRepository.GetLegacyUrlCount);
			configuration.Configuration.NamedQueries.RegisterQuery("from LegacyUrl b", KnownQueries.LegacyUrlRepository.GetLegacyUrl);
		}

		static void AddCommentRepositoryQueries ( this INHibernateHelper configuration ) {
			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(Id) from Comment where Approved = true" , KnownQueries.CommentRepository.GetApprovedWithBasicItemPropertiesCount );
			configuration.Configuration.NamedQueries.RegisterQuery("select distinct c from Comment c join fetch c.Item where Approved = true order by c.CreationDate", KnownQueries.CommentRepository.GetApprovedWithBasicItemProperties);

			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(Id) from Comment where Approved = false" , KnownQueries.CommentRepository.GetUnapprovedWithBasicItemPropertiesCount );
			configuration.Configuration.NamedQueries.RegisterQuery("select distinct c from Comment c join fetch c.Item where Approved = false order by c.CreationDate", KnownQueries.CommentRepository.GetUnapprovedWithBasicItemProperties);

			configuration.Configuration.NamedQueries.RegisterQuery("select distinct c from Comment c join fetch c.Item where Approved = true order by c.CreationDate desc", KnownQueries.CommentRepository.GetRecentCommentWithBasicItemProperties);

			configuration.Configuration.NamedQueries.RegisterQuery("from Comment c join fetch c.Item where c.Id = :key", KnownQueries.CommentRepository.GetByKeyWithBasicItemProperties);
		}

		static void AddPageRepositoryQueries ( this INHibernateHelper configuration ) {
			configuration.Configuration.NamedQueries.RegisterQuery ( "from Page p where p.Slug = :slug" , KnownQueries.PageRepository.GetBySlug  );
			configuration.Configuration.NamedQueries.RegisterQuery("from Page p left join fetch p.Parent where p.Slug = :slug", KnownQueries.PageRepository.GetBySlugWithParent);

			configuration.Configuration.NamedQueries.RegisterQuery ( "from Page p where p.Id = :key" , KnownQueries.PageRepository.GetByKey );
			configuration.Configuration.NamedQueries.RegisterQuery("from Page p left join fetch p.Parent where p.Id = :key", KnownQueries.PageRepository.GetByKeyWithParent);

			configuration.Configuration.NamedQueries.RegisterQuery ( "from PageStructure p order by p.SortOrder" , KnownQueries.PageRepository.GetPagesStructureList);
			configuration.Configuration.NamedQueries.RegisterQuery ( "select count(p.Id) from PageStructure p" , KnownQueries.PageRepository.GetPagesStructureListCount );
			configuration.Configuration.NamedQueries.RegisterQuery("select distinct p from PageStructure p left join fetch p.Pages where p.Parent = null order by p.SortOrder", KnownQueries.PageRepository.GetPagesStructureRootList);

			configuration.Configuration.NamedQueries.RegisterQuery("select distinct p from Page p left join fetch p.Parent order by p.SortOrder", KnownQueries.PageRepository.GetPagedList);
			configuration.Configuration.NamedQueries.RegisterQuery ( "select count(p.Id) from Page p", KnownQueries.PageRepository.GetPagedListCount);

			configuration.Configuration.NamedQueries.RegisterQuery ( "select p.Slug from PageStructure p" , KnownQueries.PageRepository.GetAllPagesSlugs );
		}

		static void AddReferrerRepositoryQueries ( this INHibernateHelper configuration ) {
			configuration.Configuration.NamedQueries.RegisterQuery ( "from Referrer order by CreationDate desc", KnownQueries.ReferrerRepository.GetReferrers );
			configuration.Configuration.NamedQueries.RegisterQuery ( "select count(r.Id) from Referrer r" , KnownQueries.ReferrerRepository.GetReferrersCount );
		}

		static void AddTrackBackRepositoryQueries ( this INHibernateHelper configuration ) {
			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(t.Id) from TrackBack t where t.Item.Slug = :slug and t.Url = :url" , KnownQueries.TrackBackRepository.IsFirstPingBackFromSlug );
			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(t.Id) from TrackBack t where t.Item.Id = :postKey and t.Url = :url" , KnownQueries.TrackBackRepository.IsFirstPingBackFromPostKey );

			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(t.Id) from TrackBack t " , KnownQueries.TrackBackRepository.GetTrackBackCount );
			configuration.Configuration.NamedQueries.RegisterQuery ( "from TrackBack" , KnownQueries.TrackBackRepository.GetTrackBack );


			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(t.Id) from TrackBack t where t.Item.Id = :key" , KnownQueries.TrackBackRepository.GetTrackBackForItemByKeyCount );
			configuration.Configuration.NamedQueries.RegisterQuery("from TrackBack t where t.Item.Id = :key order by t.CreationDate", KnownQueries.TrackBackRepository.GetTrackBackForItemByKey);

			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(t.Id) from TrackBack t where t.Item.Slug = :slug" , KnownQueries.TrackBackRepository.GetTrackBackForItemBySlugCount );
			configuration.Configuration.NamedQueries.RegisterQuery("from TrackBack t where t.Item.Slug = :slug order by t.CreationDate", KnownQueries.TrackBackRepository.GetTrackBackForItemBySlug);
		}

		static void AddUserRepositoryQueries ( this INHibernateHelper configuration ) {
			configuration.Configuration.NamedQueries.RegisterQuery("from User u left join fetch u.Roles where u.Id = :key", KnownQueries.UserRepository.GetByKeyWithRoles);
			configuration.Configuration.NamedQueries.RegisterQuery("from User u left join fetch u.Roles where u.Email = :email", KnownQueries.UserRepository.GetByEmailWithRoles);
			configuration.Configuration.NamedQueries.RegisterQuery("from User u left join fetch u.Roles where u.Username = :username", KnownQueries.UserRepository.GetByUsernameWithRoles);
			configuration.Configuration.NamedQueries.RegisterQuery("select distinct u from User u left join fetch u.Roles", KnownQueries.UserRepository.GetListOfUserWithRoles);
			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(t.Id) from User t" , KnownQueries.UserRepository.GetListOfUserWithRolesCount );
			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(t.Id) from User t where t.LastActivityDate > :lastActivityDate" , KnownQueries.UserRepository.GetOnlineUsers );

			configuration.Configuration.NamedQueries.RegisterQuery ( "from User u left join fetch u.Roles where u.LoweredUsername like :loweredUsername" , KnownQueries.UserRepository.FindUsersByNameWithRoles );
			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(t.Id) from User t where t.LoweredUsername like :loweredUsername" , KnownQueries.UserRepository.FindUsersByNameWithRolesCount );
			configuration.Configuration.NamedQueries.RegisterQuery("from User u left join fetch u.Roles where u.LoweredEmail like :loweredEmail", KnownQueries.UserRepository.FindUsersByEmailWithRoles);
			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(t.Id) from User t where t.LoweredEmail like :loweredEmail" , KnownQueries.UserRepository.FindUsersByEmailWithRolesCount );

			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(Id) from Role", KnownQueries.UserRepository.GetAllRolesCount );
			configuration.Configuration.NamedQueries.RegisterQuery ( "from Role order by Name", KnownQueries.UserRepository.GetAllRoles );
		}

		static void AddRoleRepositoryQueries(this INHibernateHelper configuration)
		{
			// todo: why in the hell do I have to use a fetch join to have only the users ?
			configuration.Configuration.NamedQueries.RegisterQuery("select distinct u from User u join fetch u.Roles r where r.Name = :name", KnownQueries.RoleRepository.GetUsersInRole);
			configuration.Configuration.NamedQueries.RegisterQuery("select count(r.Id) from Role r where r.Name = :name", KnownQueries.RoleRepository.Exist);
		}

		static void AddEmailRepositoryQueries ( this INHibernateHelper configuration ) {
			configuration.Configuration.NamedQueries.RegisterQuery ( "from EmailMessage order by Id desc" , KnownQueries.EmailRepository.Peek );
		}

		static void AddPostRepositoryQueries ( this INHibernateHelper configuration ) {
			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(p.Id) from Post p where p.IsPublished = true and p.PublishDate < :publishDate", KnownQueries.PostRepository.GetPublishedCount);

			configuration.Configuration.NamedQueries.RegisterQuery("select Count(p.Id) from Post p where p.IsPublished = false and p.PublishDate < :publishDate", KnownQueries.PostRepository.GetUnpublishedCount);

			// removed 'left join fetch p.Tags' left join fetch p.Categories
			configuration.Configuration.NamedQueries.RegisterQuery("select distinct p from Post p order by p.PublishDate desc", KnownQueries.PostRepository.GetCompleteList);
			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(p.Id) from Post p ", KnownQueries.PostRepository.GetCompleteListCount );

			configuration.Configuration.NamedQueries.RegisterQuery ( "from Post p where p.IsPublished = true and p.PublishDate < :publishDate order by p.PublishDate desc", KnownQueries.PostRepository.GetRecent);

			// removed 'left join fetch p.Tags' left join fetch p.Categories
			configuration.Configuration.NamedQueries.RegisterQuery("select distinct p from Post p where p.IsPublished = true and month( p.PublishDate ) = :month and year( p.PublishDate ) = :year order by p.PublishDate desc", KnownQueries.PostRepository.GetByMonth);
			configuration.Configuration.NamedQueries.RegisterQuery("select Count(p.Id) from Post p where p.IsPublished = true and month( p.PublishDate ) = :month and year( p.PublishDate ) = :year", KnownQueries.PostRepository.GetByMonthCount);

			// removed 'left join fetch p.Tags left join fectch p.TrackBacks'
			configuration.Configuration.NamedQueries.RegisterQuery("select distinct p from Post p left join fetch p.Categories where p.Slug = :slug", KnownQueries.PostRepository.GetBySlug);

			configuration.Configuration.NamedQueries.RegisterQuery("from Post p where p.IsPublished = false and p.PublishDate >:minDate and p.PublishDate < :maxDate order by p.PublishDate", KnownQueries.PostRepository.GetUnpublishedForTheSpecifiedDate);
			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(p.Id) from Post p where p.IsPublished = false and p.PublishDate >:minDate and p.PublishDate < :maxDate", KnownQueries.PostRepository.GetUnpublishedForTheSpecifiedDateCount );

			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(p.Id) from Post p where p.IsPublished = true and p.PublishDate >:minDate and p.PublishDate < :maxDate", KnownQueries.PostRepository.GetPublishedPostForSpecifiedPublishDateRangeCount );
			configuration.Configuration.NamedQueries.RegisterQuery("from Post p where p.IsPublished = true and p.PublishDate >:minDate and p.PublishDate < :maxDate order by p.PublishDate", KnownQueries.PostRepository.GetPublishedPostForSpecifiedPublishDateRange);

			// removed 'left join fetch p.Tags'
			configuration.Configuration.NamedQueries.RegisterQuery ( "select distinct p from Post p left join fetch p.Categories t where p.Id = :id", KnownQueries.PostRepository.GetByKey);
		}

		static void AddTagRepositoryQueries(this INHibernateHelper configuration) {
			// removed 'fetch'
			configuration.Configuration.NamedQueries.RegisterQuery ( "select distinct p from Item p left join fetch p.Tags t left join p.Categories where t.Name = :tagName and p.IsPublished = true and p.PublishDate < :publishDate order by p.PublishDate desc", KnownQueries.TagRepository.GetItemsByTag);
			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(p.Id) from Item p join p.Tags t where t.Name = :tagName and p.IsPublished = true and p.PublishDate < :publishDate", KnownQueries.TagRepository.GetItemsByTagCount);
			// removed 'fetch'
			configuration.Configuration.NamedQueries.RegisterQuery ( "select distinct p from Post p join fetch p.Tags t left join p.Categories where t.Name = :tagName and p.IsPublished = true and p.PublishDate < :publishDate order by p.PublishDate desc", KnownQueries.TagRepository.GetPostsByTag);
			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(p.Id) from Post p join p.Tags t where t.Name = :tagName and p.IsPublished = true and p.PublishDate < :publishDate", KnownQueries.TagRepository.GetPostsByTagCount);
		}

		static void AddInboxRepositoryQueries(this INHibernateHelper configuration) {
			configuration.Configuration.NamedQueries.RegisterQuery ( "select Count(Id) from InboxMessage", KnownQueries.InboxRepository.GetListCount );
			configuration.Configuration.NamedQueries.RegisterQuery ( "from InboxMessage i order by i.MessageDate desc", KnownQueries.InboxRepository.GetListOrdedByMessageDate );
		}

		static void AddPluginRepositoryQueries(this INHibernateHelper configuration)
		{
			configuration.Configuration.NamedQueries.RegisterQuery("from Plugin p where p.Identifier = :identifier", KnownQueries.PluginRepository.GetPluginByIdentifier);
			configuration.Configuration.NamedQueries.RegisterQuery("from PluginData p where p.Parent.Id = :pluginId and p.Key = :key", KnownQueries.PluginRepository.RetrieveDataFromKey);
			configuration.Configuration.NamedQueries.RegisterQuery("from PluginData p where p.Parent.Id = :pluginId and p.CreationDate > :startDate and p.CreationDate < :endDate order by p.CreationDate desc", KnownQueries.PluginRepository.RetrieveDataForSpecifiedDateRange);
			configuration.Configuration.NamedQueries.RegisterQuery("select count(Id) from PluginData p where p.Parent.Id = :pluginId and p.CreationDate > :startDate and p.CreationDate < :endDate", KnownQueries.PluginRepository.RetrieveDataForSpecifiedDateRangeCount);
		}
	}
}